﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="GuidSettingDescriptor.cs" company="Radical Research Ltd">
//   Copyright (c) Radical Research Ltd. The MIT License (MIT)
// </copyright>
// <summary>
//   
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace RadicalResearch.MiniState.Descriptors
{
    using System;

    /// <summary>Reads and writes GUID values.</summary>
    internal class GuidSettingDescriptor : StringSettingDescriptor
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="GuidSettingDescriptor"/> class.
        /// </summary>
        /// <param name="valueKey">The valueKey used to identify the value in the value collection.</param>
        /// <param name="isValidated">if set to <c>true</c> value is validated with hash.</param>
        public GuidSettingDescriptor(string valueKey, bool isValidated)
            : base(valueKey, isValidated)
        {
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Reads the value.
        /// </summary>
        /// <param name="values">The values.</param>
        /// <returns>
        /// value as an object
        /// </returns>
        public override object ReadValue(IValueCollection values)
        {
            string guidString = (string)base.ReadValue(values);
            byte[] guidBytes = Convert.FromBase64String(guidString);
            return new Guid(guidBytes);
        }

        /// <summary>
        /// Writes the value to the collection.
        /// </summary>
        /// <param name="values">The value collection to write to.</param>
        /// <param name="value">The value to write.</param>
        public override void WriteValue(IValueCollection values, object value)
        {
            Guid guidValue = (Guid)value;
            string guidString = Convert.ToBase64String(guidValue.ToByteArray());
            base.WriteValue(values, guidString);
        }

        #endregion
    }
}